package com.javaweb.dao.impl;

import com.javaweb.dao.CommentDao;
import com.javaweb.model.Article;
import com.javaweb.model.Comment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.util.ObjectUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * @desc:
 * @author: Shoko
 * @date: 2024/10/22
 */
@Repository
public class CommentDaoImpl implements CommentDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    private final RowMapper<Comment> commentRowMapper = new RowMapper<Comment>() {
        @Override
        public Comment mapRow(ResultSet rs, int rowNum) throws SQLException {
            Comment comment = new Comment();
            comment.setId(rs.getInt("id"));
            comment.setContent(rs.getString("content"));
            comment.setArticleId(rs.getInt("article_id"));
            comment.setUserId(rs.getInt("user_id"));
            comment.setCreateTime(rs.getTimestamp("create_time"));
            comment.setUpdateTime(rs.getTimestamp("update_time"));
            comment.setSupport(rs.getInt("support"));
            comment.setOppose(rs.getInt("oppose"));
            return comment;
        }
    } ;

    //    （10）添加新的评论：int insert(Comment newComment)
    @Override
    public int insert(Comment newComment) {
        if(ObjectUtils.isEmpty(newComment) || ObjectUtils.isEmpty(newComment.getArticleId())){
            return 0;
        }
        String sql="insert into tb_comment(content,article_id,user_id,support,oppose,create_time,update_time) values(?,?,?,?,?,?,?)";
        Object[] args=new Object[]{newComment.getContent(),newComment.getArticleId(),newComment.getUserId(),
        newComment.getSupport(),newComment.getOppose(),newComment.getCreateTime(),newComment.getUpdateTime()};

        return jdbcTemplate.update(sql,args)>0?1:0;
    }
    //    （11）点赞： int insertSupport(Integer commentId)
    @Override
    public int insertSupport(Integer commentId) {
        if(ObjectUtils.isEmpty(commentId)){
            return 0;
        }
        String sql="update tb_comment set support=support+1 where id=?";
        Object[] args=new Object[]{commentId};
        return jdbcTemplate.update(sql,args)>0?1:0;
    }
    //    （12）点差： int insertOppose(Integer commentId)
    @Override
    public int insertOppose(Integer commentId) {
        if(ObjectUtils.isEmpty(commentId)){
            return 0;
        }
        String sql="update tb_comment set oppose=oppose+1 where id=?";
        Object[] args=new Object[]{commentId};
        return jdbcTemplate.update(sql,args)>0?1:0;
    }
    //    （13）查询文章对应的所有评论：List selectAllCommentsById(Integer articleId)
    @Override
    public List<Comment> selectAllCommentsById(Integer articleId) {
        String sql="select * from tb_comment where article_id=?";
        Object[] args=new Object[]{articleId};
        return jdbcTemplate.query(sql,args,commentRowMapper);
    }
}
